/**
 * 
 */
package com.java.test.audit.mapper;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import com.java.test.audit.dto.RemitExamineInfo;
import com.java.test.util.PageInfo;

/**
 * @author Administrator
 *
 */
@Component
public class RemitExamineMapper {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	public int selectCount(RemitExamineInfo info) {
		String sql = "select count(*) from examine_remit where 1=1 ";

		if (info.getRemitStatus() != 0) {
			sql += " and remit_status='" + info.getRemitStatus() + "'";
		}

		if (info.getRemitChannelValue() != 0) {
			sql += " and remit_channelvalue ='" + info.getRemitChannelValue() + "'";
		}

		if (info.getRemitTime() != null) {
			sql += " and remit_time >='" + info.getRemitTime() + "'";
		}

		if (info.getGameAccount() != null && !"".equals(info.getGameAccount())) {
			sql += " and game_account like '%" + info.getGameAccount() + "%'";
		}

		Integer i = jdbcTemplate.queryForObject(sql, Integer.class);

		return i == null ? null : i;

	}

	public List<RemitExamineInfo> selectpageList(PageInfo<RemitExamineInfo> page, RemitExamineInfo info) {
		String sql = "select * from examine_remit where 1=1 ";

		if (info.getRemitStatus() != 0) {
			sql += " and remit_status='" + info.getRemitStatus() + "'";
		}

		if (info.getRemitChannelValue() != 0) {
			sql += " and remit_channelvalue ='" + info.getRemitChannelValue() + "'";
		}

		if (info.getRemitTime() != null) {
			sql += " and remit_time >='" + info.getRemitTime() + "'";
		}

		if (info.getGameAccount() != null && !"".equals(info.getGameAccount())) {
			sql += " and game_account like '%" + info.getGameAccount() + "%'";
		}
		int start = (page.getCurrentPage() - 1) * page.getPageCount();
		int limit = page.getPageCount();

		sql += " order by remit_status desc,remit_time desc limit ?,?";
		List<RemitExamineInfo> list = jdbcTemplate.query(sql, new RemitExamineInfo(), start, limit);
		return list;
	}

	public int updateremitExaminInfo(int id) {
		String sql = "update examine_remit set remit_status =1 where id=?";
		int i = jdbcTemplate.update(sql, id);
		return i;
	}
	
	
	public RemitExamineInfo selectremitInfoById(int id) {
		String sql  ="select * from examine_remit where id =?";
		return jdbcTemplate.queryForObject(sql, new RemitExamineInfo(),id);	
	}
	
	

	public int insertplRemitInfo(List<RemitExamineInfo> info) {

		String sql = "INSERT INTO examine_remit "
				+ "(`game_account`, `remit_amount`, `remit_channelvalue`, "
				+ "`remit_channelname`, `remit_time`, `remit_bankaccount`, "
				+ "`audit_status`, `reviewer`, `remit_status`,audit_time,order_number,content,game_unique_id,sxf_charge,dzje_amount) "
				+ "VALUES ( ?, ?, ?, ?, ?, ?, ?,?,?,?,?,?,?,?,?)";
		jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
			public int getBatchSize() {
				return info.size();
				// 这个方法设定更新记录数，通常List里面存放的都是我们要更新的，所以返回list.size();
			}

			@SuppressWarnings("static-access")
			public void setValues(PreparedStatement ps, int i) throws SQLException {
				ps.setString(1, info.get(i).getGameAccount());
				ps.setDouble(2, info.get(i).getRemitAmount());
				ps.setInt(3, info.get(i).getRemitChannelValue());
				ps.setString(4, info.get(i).getRemitChannelName());
				ps.setTimestamp(5, Timestamp.valueOf(info.get(i).getRemitTime().now()));
				ps.setString(6, info.get(i).getRemitbankAccount());
				ps.setInt(7, info.get(i).getAuditStatus());
				ps.setString(8, info.get(i).getReviewer());
				ps.setInt(9, info.get(i).getRemitStatus());
				ps.setTimestamp(10, Timestamp.valueOf(info.get(i).getRemitTime().now()));
				ps.setString(11, info.get(i).getOrderNumber());
				ps.setString(12, info.get(i).getContent());
				ps.setString(13, info.get(i).getGame_unique_id());
				ps.setDouble(14, info.get(i).getSxfCharge());
				ps.setDouble(15, info.get(i).getDzjeAmount());
			}
		});

		return 1;
	}

}
